package com.sp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sp.model.dto.PermissionDTO;
import com.sp.model.entity.PageResult;
import com.sp.model.entity.Permission;
import com.sp.model.entity.Result;
import com.sp.model.vo.PermissionVO;
import com.sp.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sp.constant.PermissionMessageConstant.*;

/**
 * @author tong
 * @date 2024/4/14 13:50
 */
@Api(tags = "具体权限管理api接口")
@RestController
@RequestMapping("/permissions")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("添加具体权限")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'permission:add')")
    @PostMapping
    public Result<Object> addPermission(
            @ApiParam(name = "permissionDTO", value = "具体权限信息") @RequestBody PermissionDTO permissionDTO) {
        if (permissionService.addPermission(permissionDTO)) {
            return Result.success(ADD_PERMISSION_SUCCESS);
        }
        return Result.error(ADD_PERMISSION_FAIL);
    }

    @ApiOperation("修改具体权限")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'permission:update')")
    @PutMapping
    public Result<Object> updatePermission(
            @ApiParam(name = "permissionDTO", value = "具体角色信息") @RequestBody PermissionDTO permissionDTO) {
        if (permissionService.updatePermission(permissionDTO)) {
            return Result.success(UPDATE_PERMISSION_SUCCESS);
        }
        return Result.error(UPDATE_PERMISSION_FAIL);
    }

    @ApiOperation("删除具体权限")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'permission:delete')")
    @DeleteMapping("/{ids}")
    public Result<Object> deletePermission(
            @ApiParam(name = "ids", value = "具体权限id列表") @PathVariable("ids") List<Integer> ids) {
        if (permissionService.deletePermissions(ids)) {
            return Result.success(DELETE_PERMISSION_SUCCESS);
        }
        return Result.error(DELETE_PERMISSION_FAIL);
    }

    @ApiOperation("条件查询具体权限")
    @GetMapping("/page")
    public Result<PageResult<PermissionVO>> page(
            @ApiParam(name = "currentPage", value = "页码") Integer currentPage,
            @ApiParam(name = "pageSize", value = "每页条数") Integer pageSize,
            @ApiParam(name = "permissionDTO", value = "具体权限") PermissionDTO permissionDTO) {
        IPage<Permission> pages = permissionService.page(currentPage, pageSize, permissionDTO);
        if (pages != null) {
            List<PermissionVO> permissionVOS = BeanUtil.copyToList(pages.getRecords(), PermissionVO.class);

            return Result.success(SELECT_PERMISSION_SUCCESS
                    , new PageResult<>(pages.getSize(), pages.getTotal(), pages.getCurrent(), pages.getPages(), permissionVOS));
        }
        return Result.error(SELECT_PERMISSION_FAIL);
    }

    @ApiOperation("查询所有具体权限")
    @GetMapping("/selectAll")
    public Result<List<PermissionVO>> selectAll() {
        List<Permission> permissions = permissionService.selectAll();
        if (permissions == null || permissions.size() == 0) {
            return Result.success("具体权限数据查询完毕，但数据为空，请联系管理员处理");
        }

        List<PermissionVO> permissionVOS = BeanUtil.copyToList(permissions, PermissionVO.class);

        return Result.success(SELECT_PERMISSION_SUCCESS, permissionVOS);
    }

}
